//
//  SearchComPanyViewController.m
//  Logistics
//
//  Created by hy002 on 2018/1/12.
//  Copyright © 2018年 HUYU. All rights reserved.
//

#import "SearchComPanyViewController.h"
#import "DetailsModel.h"
#import "LMJScrollTextView2.h"
#import "CompanyHomeViewController.h"

@interface SearchComPanyViewController ()<UITextFieldDelegate,LMJScrollTextView2Delegate>
{
    UIView *backView;
    UIView *numView;
    LMJScrollTextView2 * _scrollTextView;
}
@property (nonatomic, strong)UITextField *nameField;
@property (nonatomic, strong)NSArray *comPanyAry;

@end

@implementation SearchComPanyViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (_comPanyAry.count > 0)
    {
        [_scrollTextView startScrollBottomToTopWithNoSpace];
    }
    [self requestCountTheSearch];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [_scrollTextView stop];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatUI];
    [self requestCompanySearch];
}

- (void)creatUI
{
    self.navigationItem.title = @"公司搜索";
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT)];
    backView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:backView];
    UIImageView *imgView = [ZCControl createImageViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 320*displaywidth) ImageName:@"search"];
    imgView.contentMode = UIViewContentModeScaleAspectFill;
    imgView.layer.masksToBounds = YES;
    [backView addSubview:imgView];
    numView = [[UIView alloc] initWithFrame:CGRectMake(0, 340*displaywidth, SCREEN_WIDTH, 25*displaywidth)];
    [backView addSubview:numView];
    UIView *grayView = [[UIView alloc] initWithFrame:CGRectMake(35*displaywidth, 380*displaywidth, SCREEN_WIDTH-70*displaywidth, 45*displaywidth)];
    grayView.backgroundColor = [UIColor whiteColor];
    grayView.layer.cornerRadius = 5;
    grayView.layer.masksToBounds = YES;
    grayView.layer.borderColor = RGB(153, 153, 153).CGColor;
    grayView.layer.borderWidth = 1;
    [backView addSubview:grayView];
    UIImageView *leftImgView = [ZCControl createImageViewWithFrame:CGRectMake(15*displaywidth, 10*displaywidth, 25*displaywidth, 25*displaywidth) ImageName:@"sy_search"];
    leftImgView.contentMode = UIViewContentModeScaleAspectFit;
    [grayView addSubview:leftImgView];
    UIView *lineAview = [[UIView alloc] initWithFrame:CGRectMake(45*displaywidth, 12.5*displaywidth, 1, 20*displaywidth)];
    lineAview.backgroundColor = RGB(180, 180, 180);
    [grayView addSubview:lineAview];
    self.nameField = [ZCControl createTextFieldWithFrame:CGRectMake(50*displaywidth, 0, SCREEN_WIDTH-130*displaywidth, 45*displaywidth) placeholder:@"输入公司全称" passWord:NO leftImageView:nil rightImageView:nil Font:16.0];
    _nameField.delegate = self;
    _nameField.returnKeyType = UIReturnKeySearch;
    [grayView addSubview:_nameField];
    UILabel *ruLab = [ZCControl createLabelWithFrame:CGRectMake(35*displaywidth, 440*displaywidth, 35*displaywidth, 25*displaywidth) Font:16.0 Text:@"如：" Color:RGB(169, 178, 196)];
    ruLab.textAlignment = NSTextAlignmentCenter;
    [backView addSubview:ruLab];
    _scrollTextView = [[LMJScrollTextView2 alloc] initWithFrame:CGRectMake(70*displaywidth, 440*displaywidth, SCREEN_WIDTH-85*displaywidth, 25*displaywidth)];
    _scrollTextView.delegate        = self;
    _scrollTextView.backgroundColor = [UIColor whiteColor];
    _scrollTextView.textColor       = RGB(169, 178, 196);
    _scrollTextView.textFont        = [UIFont systemFontOfSize:16.0f];
    _scrollTextView.textAlignment   = NSTextAlignmentLeft;
    _scrollTextView.touchEnable     = YES;
    [backView addSubview:_scrollTextView];
    UIButton *soureBtn = [ZCControl createButtonWithFrame:CGRectMake(15*displaywidth, SCREEN_HEIGHT-STA_NAV_HEIGHT-SCREER_HeiX-55*displaywidth, SCREEN_WIDTH-30*displaywidth, 45*displaywidth) ImageName:nil Target:self Action:@selector(soureBtnAction) Title:@"查询" Font:16.0];
    soureBtn.backgroundColor = RGB(255, 153, 0);
    [soureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    soureBtn.layer.cornerRadius = 5;
    soureBtn.layer.masksToBounds = YES;
    [backView addSubview:soureBtn];
}

- (void)creatNumbelView:(NSString *)str
{
    for (id v in numView.subviews)
    {
        [v removeFromSuperview];
    }
    for (int i = 0; i < 10; i++)
    {
        if (i > 0 && i < 9)
        {
            NSRange range = NSMakeRange(i-1, 1);
            UILabel *numLab = [ZCControl createLabelWithFrame:CGRectMake(75*displaywidth+(i-1)*30*displaywidth, 0, 25*displaywidth, 25*displaywidth) Font:16.0 Text: [str substringWithRange:range] Color:[UIColor whiteColor]];
            numLab.textAlignment = NSTextAlignmentCenter;
            numLab.backgroundColor = RGB(255, 153, 51);
            [numView addSubview:numLab];
        }
        else
        {
            CGFloat X = 15*displaywidth;
            NSString *name = @"已查询";
            if (i != 0)
            {
                X = 75*displaywidth+240*displaywidth;
                name = @"次";
            }
            UILabel *tostLab = [ZCControl createLabelWithFrame:CGRectMake(X, 0, 55*displaywidth, 25*displaywidth) Font:12.0 Text:name Color:RGB(255, 153, 51)];
            if (i == 0)
            {
                tostLab.textAlignment = NSTextAlignmentRight;
            }
            [numView addSubview:tostLab];
        }
    }
}

- (void)requestCountTheSearch
{
    WEAKSELF;
    [HYBaseHandler requestWithTheFormUrl:SERVER_companyQueryCount Parmers:nil Success:^(id obj) {
        if ([obj server_Getcodecheck])
        {
            NSString *numStr = obj[@"data"];
            NSInteger num = [numStr integerValue];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf creatNumbelView:[NSString stringWithFormat:@"%08ld",num]];
            });
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        
    }];
}

- (void)requestCompanySearch
{
    WEAKSELF;
    NSDictionary *dic = @{@"size":@"10",
                          @"zheng":@"1"
                          };
    [HYBaseHandler requestWithTheFormUrl:SERVER_companySearch Parmers:dic Success:^(id obj) {
        if ([obj server_Getcodecheck])
        {
            _comPanyAry = [DetailsModel mj_objectArrayWithKeyValuesArray:obj[@"data"][@"content"]];
            dispatch_async(dispatch_get_main_queue(), ^{
                NSMutableArray *nameArray = [NSMutableArray arrayWithCapacity:0];
                for (DetailsModel *model in _comPanyAry)
                {
                    [nameArray addObject:model.companyName];
                }
                _scrollTextView.textDataArr = nameArray;
                [_scrollTextView startScrollBottomToTopWithNoSpace];
            });
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        
    }];
    
}

- (void)soureBtnAction
{
    WEAKSELF;
    if ([_nameField.text isEqualToString:@""])
    {
        [LTools showMBProgressWithText:@"请输入公司全名" addToView:self.view];
        return;
    }
    NSDictionary *dic = @{@"companyName":_nameField.text};
    [HYBaseHandler requestWithTheFormUrl:SERVER_companyNameSearch Parmers:dic Success:^(id obj) {
        if ([obj server_Getcodecheck])
        {
            DetailsModel *model = [DetailsModel mj_objectWithKeyValues:obj[@"data"]];
            if ([model.ID integerValue] > 0)
            {
                CompanyHomeViewController *vc = [[CompanyHomeViewController alloc] init];
                vc.comPanyId = s_str(model.ID);
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        
    }];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    backView.frame = CGRectMake(0, -100, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT);
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    backView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT);

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    if (![textField.text isEqualToString:@""])
    {
        [self soureBtnAction];
    }
    return YES;
}

- (void)scrollTextView2:(LMJScrollTextView2 *)scrollTextView clickIndex:(NSInteger)index content:(NSString *)content
{
    _nameField.text = content;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
